"""
---------------------------------------------
    PROJECT:        flaskforfilm
    FILE NAME :     models
    DESCRIPTION:
    AUTHOR:         python
---------------------------------------------

@卫佳陆
"""
from datetime import datetime
from app import db


# from flask import Flask
# from flask.ext.sqlalchemy import SQLAlchemy
#
# app = Flask(__name__)
# app.config['SQLALCHEMY_DATABASE_URI'] = "mysql://root:mysql@127.0.0.1:3306/movie"
# app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
# db = SQLAlchemy(app)


class User(db.Model):
    # 用户信息表
    __tablename__ = "users"  # 表名
    id = db.Column(db.Integer, primary_key=True)  # id
    name = db.Column(db.String(64), unique=True)  # 用户名
    pwd = db.Column(db.String(64))  # 密码
    email = db.Column(db.String(64), unique=True)  # 电邮
    phone = db.Column(db.String(11), unique=True)  # 电话
    info = db.Column(db.Text)  # 简介
    face = db.Column(db.String(255), unique=True)  # 头像
    addtime = db.Column(db.DateTime, index=True, default=datetime.utcnow)  # 注册时间
    uuid = db.Column(db.String(255), unique=True)  # 唯一辨识符
    userlogs = db.relationship("Userlog", backref='user')  # 与用户日志相关联
    comments = db.relationship("Comment", backref='user')  # 与评论关联
    moviecols = db.relationship("Moviecol", backref='user')

    def __repr__(self):
        return "<User %r>" % self.name

    def check_pwd(self, pwd):
        from werkzeug.security import check_password_hash
        return check_password_hash(self.pwd, pwd)


class Userlog(db.Model):
    # 用户日志
    __tablename__ = "userlogs"
    id = db.Column(db.Integer, primary_key=True)  # id
    user_id = db.Column(db.Integer, db.ForeignKey("users.id"))  # 用外键与用户表相关联
    ip = db.Column(db.String(100))  # 登陆ip
    addtime = db.Column(db.DateTime, index=True, default=datetime.utcnow)  # 登陆时间

    def __repr__(self):
        return "<Userlog %r>" % self.id


class Tag(db.Model):
    # 标签
    __tablename__ = "tags"
    id = db.Column(db.Integer, primary_key=True)  # id
    name = db.Column(db.String(100), unique=True)  # 标签名
    addtime = db.Column(db.DateTime, index=True, default=datetime.utcnow)  # 添加时间
    movies = db.relationship("Movie", backref="tags")

    def __repr__(self):
        return "<Tag %r>" % self.name


class Movie(db.Model):
    # 电影
    __tablename__ = "movies"
    id = db.Column(db.Integer, primary_key=True)  # 编号
    title = db.Column(db.String(255), unique=True)  # 电影名
    url = db.Column(db.String(255), unique=True)  # 地址
    info = db.Column(db.Text)  # 简介
    logo = db.Column(db.String(255), unique=True)  # 封面
    star = db.Column(db.SmallInteger)  # 星级
    playnum = db.Column(db.BigInteger)  # 播放量
    commentnum = db.Column(db.BigInteger)  # 评论量
    tag_id = db.Column(db.Integer, db.ForeignKey("tags.id"))  # 标签号
    area = db.Column(db.String(255))  # 上映地区
    release_time = db.Column(db.Date)  # 上映时间
    length = db.Column(db.String(100))  # 时长
    addtime = db.Column(db.DateTime, index=True, default=datetime.utcnow)  # 添加时间
    comments = db.relationship("Comment", backref="movie")  # 和评论表建立关系
    moviecols = db.relationship("Moviecol", backref='movie')  # 和电影收藏关联

    def __repr__(self):
        return "<Movie %r>" % self.title


class Preview(db.Model):
    # 上映预告
    __tablename__ = "previews"
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(255), unique=True)  # 电影名Preview
    addtime = db.Column(db.DateTime, index=True, default=datetime.utcnow)  # 添加时间

    def __repr__(self):
        return "<Preview %r>" % self.title


class Comment(db.Model):
    # 评论
    __tablename__ = "comments"
    id = db.Column(db.Integer, primary_key=True)
    conten = db.Column(db.Text)
    movie_id = db.Column(db.Integer, db.ForeignKey("movies.id"))  # 指向电影表的外键
    user_id = db.Column(db.Integer, db.ForeignKey("users.id"))  # 指向用户表的外键
    addtime = db.Column(db.DateTime, index=True, default=datetime.utcnow)  # 添加时间

    def __repr__(self):
        return "<Comment %r>" % self.id


class Moivecol(db.Model):
    # 收藏数
    __tablename__ = "moviecols"
    id = db.Column(db.Integer, primary_key=True)
    movie_id = db.Column(db.Integer, db.ForeignKey("movies.id"))  # 指向电影表的外键
    user_id = db.Column(db.Integer, db.ForeignKey("users.id"))  # 指向用户表的外键
    addtime = db.Column(db.DateTime, index=True, default=datetime.utcnow)  # 添加时间

    def __repr__(self):
        return "<Moviecol %r>" % self.id


'''
===========================================前后台分割线===============================================
'''''


class Auth(db.Model):
    # 权限
    __tablename__ = "auths"
    id = db.Column(db.Integer, primary_key=True)  # 编号
    name = db.Column(db.String(100), unique=True)  # 名称
    url = db.Column(db.String(255), unique=True)  # 地址
    addtime = db.Column(db.DateTime, index=True, default=datetime.utcnow)  # 添加时间

    def __repr__(self):
        return "<Auth %r>" % self.name


class Role(db.Model):
    # 角色
    __tablename__ = 'roles'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(100), unique=True)
    auths = db.Column(db.String(600))
    addtime = db.Column(db.DateTime, index=True, default=datetime.utcnow)  # 添加时间
    admins = db.relationship("Admin", backref='role')

    def __repr__(self):
        return "<Role %r>" % self.name


class Admin(db.Model):
    # 管理员
    __tablename__ = 'admins'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(100), unique=True)
    pwd = db.Column(db.String(100))
    is_super = db.Column(db.Boolean)
    role_id = db.Column(db.Integer, db.ForeignKey('roles.id'))
    addtime = db.Column(db.DateTime, index=True, default=datetime.utcnow)  # 添加时间
    adminlogs = db.relationship("Adminlog", backref='admin')
    oplogs = db.relationship("Oplog", backref='admin')

    def __repr__(self):
        return "<Admin %r>" % self.name

    def check_pwd(self, pwd):
        from werkzeug.security import check_password_hash
        return check_password_hash(self.pwd, pwd)


class Adminlog(db.Model):
    # 管理员登陆日志
    __tablename__ = "adminlogs"
    id = db.Column(db.Integer, primary_key=True)
    admin_id = db.Column(db.Integer, db.ForeignKey("admins.id"))
    ip = db.Column(db.String(100))
    addtime = db.Column(db.DateTime, index=True, default=datetime.utcnow)  # 添加时间

    def __repr__(self):
        return "<Adminlog %r>" % self.id


class Oplog(db.Model):
    # 操作日志
    __tablename__ = "oplogs"
    id = db.Column(db.Integer, primary_key=True)
    admin_id = db.Column(db.Integer, db.ForeignKey("admins.id"))
    ip = db.Column(db.String(100))  # 此处ip是否与登陆ip重复
    reason = db.Column(db.String(600))
    addtime = db.Column(db.DateTime, index=True, default=datetime.utcnow)  # 添加时间

    def __repr__(self):
        return "<Oplog %r>" % self.id
